<template>
	<view>
		<view class="acea-row row-middle row-between nav-bg">
			<!-- <scroll-view scroll-x="true" style="white-space: nowrap; display: flex;" scroll-with-animation :scroll-left="tabLeft" show-scrollbar="true">
				<view class="item" :style='"width:"+isWidth+"px"' :data-index="index" :class="index===categoryOn?'item-on':''" v-for="(item,index) in category" :key="index" :id="'id'+index" @click="categoryNavClick(index)">
					<view class="acea-row row-middle row-center img">
						<image :src="'../static/images/'+item.image" mode=""></image>
					</view>	 
					<view class="name">
						{{item.name}}
					</view>
				</view>
			</scroll-view> -->
			<!-- 			<view class="item" :class="index===categoryOn?'item-on':''" v-for="(item,index) in categoryList" :key="index" @click="categoryNavClick(item,index)">
				<view class="acea-row row-middle row-center img">
					<image :src="item.image" mode=""></image> -->
			<!-- <image src="../static/images/nav_01.png" mode=""></image>
					<image src="../static/images/nav_02.png" mode="" v-if="index==1"></image>
					<image src="../static/images/nav_03.png" mode="" v-if="index==2"></image>
					<image src="../static/images/nav_04.png" mode="" v-if="index==3"></image>
					<image src="../static/images/nav_05.png" mode="" v-if="index==4"></image>
					<image src="../static/images/nav_06.png" mode="" v-if="index==5"></image> -->
			<!-- 			</view>	  
				<view class="name">
					{{item.name}}
				</view>
			</view> -->
			<scroll-view scroll-x="true" style="white-space: nowrap; display: flex;" scroll-with-animation
				:scroll-left="tabLeft" show-scrollbar="true">
				<view class="item" :class="index==categoryOn?'item-on':''" v-for="(item,index) in categoryList"
					:style='"width:"+isWidth+"px"' :key="index" @click="categoryNavClick(item,index)">
					<view class="acea-row row-middle row-center img">
						<image :src="item.icon" mode=""></image>
					</view>
					<view class="name">
						{{item.name}}
					</view>
				</view>
			</scroll-view>
		</view>
		<!-- 出售 求购  交换 -->
		<view class="acea-row row-middle type-bg">
			<view class="type" :class="typeOn==index?'type-on':''" v-for="(item,index) in types" :key="index"
				@click="typeNavClick(item,index)">
				{{item.name}}
			</view>
		</view>
		<view style="padding: 12rpx;"></view>
		<view class="list">
			<view class="acea-row row-middle row-between item border-b ">
				<view class="label label-m">
					商品类别
				</view>
				<view class="acea-row row-middle row-right item-r">
					<!-- <input class="input-r" type="text" placeholder="请输入商品类别" placeholder-class="placeholder-class"  v-model="goodsCate" /> -->
					<picker @change="bindPickerChange" :value="index" :range="itemList" range-key="name">
						<input class="input-r" type="text" placeholder="请输入商品类别" placeholder-class="placeholder-class"
							v-model="dataForm.categorySonName" name="categorySonName" disabled />
					</picker>
				</view>
			</view>
			<!-- 添加商品 -->
			<view class="acea-row row-middle row-between item">
				<view class="label label-m">
					添加商品
				</view>
				<view class="add-icon" @click="bindShowPopup">
					<image src="../static/images/add_icon.png" mode=""></image>
				</view>
			</view>
			<view class="acea-row goods-list border-b">
				<view class="goods" v-for="(item,index) in dataForm.products" :key="index" @click="toEditProudct(item,index)">
					<view class="img">
						<image :src="item.image?item.image.split(',')[0]:noImageShow" mode=""></image>
					</view>
					<view class="title line1">{{ item.name }}</view>
					<view class="price line1">￥{{item.price}}</view>
				</view>
			</view>
			<view class="acea-row row-middle row-between item border-b ">
				<view class="label label-m">
					交易地址
				</view>
				<view class="acea-row row-middle row-right item-r address">
					<input type="text" placeholder="请输入/选择交易地址" v-model="dataForm.address">
					<image src="../static/images/location.png" mode=""  @click="clickMap"></image>
				</view>
			</view>
			<!-- 联系人 -->
			<view class="acea-row row-middle row-between item border-b ">
				<view class="label label-m">
					联系人
				</view>
				<view class="add-icon">
					<image src="../static/images/add_icon.png" mode=""></image>
				</view>
			</view>
			<view class="item border-b">
				<chooseContact :list="contactList" :dataform="dataForm" @add="d=>contactList.push(d)" @del="toDel" />
			</view>
			<view class="acea-row row-middle row-between item border-b ">
				<view class="label label-m">
					标题
				</view>
				<view class="acea-row row-middle row-right item-r">
					<input class="input-r" type="text" placeholder="请输入标题" placeholder-class="placeholder-class"
						v-model="dataForm.title" />
				</view>
			</view>
		</view>

		<unused-popup v-if="showPopup" @close="close" :isEdit="productIsEdit" :init="initData" @toAdd = "toAddMeth" @toEdit="toEditMeth"></unused-popup>
		<view class="submit-btn" @click="saveUnused">完成</view>
		<view class="uni-p-b-98"></view>
	</view>
</template>

<script>
	// import {
	// 	getCategoryFirst
	// } from '@/api/api.js'
	import {
		getclassificationListApi
	} from '@/api/brand/index.js'
	import unusedPopup from '../components/unusedPopup/unusedPopup.vue'
	import chooseContact from '../components/chooseContact.vue'
	import {
		toPage
	} from '@/filters/commFilter.js'
	import {
		addUnused,
		editUnused,
		getUnusedDetail
	} from '@/api/unused.js'
	export default {
		components: {
			unusedPopup,
			chooseContact
		},
		data() {
			return {
				showPopup: false,
				isWidth: 40,
				isLeft: 0,
				tabLeft: 0,
				types: [{
					name: '出售',
					type: 0,
				}, {
					name: '求购',
					type: 1,
				}],
				productIsEdit:0,
				typeOn: 0,
				noImageShow: this.$noImageShow,
				categoryList: [],
				categoryOn: 0,
				goodsList: [],
				goodsCate: '', //商品类别
				dataForm: {
					products: []
				},
				itemList: [],
				index: 0,
				transactionAddress: '', //交易地址
				contactList: [],
				title: '',
				maxLength: 200,
				supplementText: '', //补充详情
				initData:{},
				editId: ''
			};
		},
		computed:{
			unsedStatus(){
				return this.types[this.typeOn].type
			}
		},
		onLoad(option) {
			this.editId = option.id ? option.id : '';
			if(option.categoryOn){
				this.categoryOn = option.categoryOn
			}
			if(option.typeOn){
				this.typeOn = option.typeOn
			}
			this.getCategoryListMeth();
			this.dataForm.city = this.$cityCode ? this.$cityCode[1] : '';
			this.dataForm.province = this.$cityCode ? this.$cityCode[0] : '';
			this.dataForm.district = this.$cityCode ? this.$cityCode[2] : '';
			this.dataForm.address = this.$address;
			console.log('dataForm.address',this.dataForm.address);
			getApp().globalData.initContact();
			if(this.editId){         
				this.getUnusedDetailMeth(this.editId)          
			}
		},
		created() {
			var that = this
			// 获取设备宽度
			uni.getSystemInfo({
				success(e) {
					that.isWidth = e.windowWidth / 6
				}
			})
		},
		methods: {
			toPage,
			bindShowPopup() {
				this.productIsEdit = 0;
				this.showPopup = true
			},
			toEditProudct(data,index){
				this.productIsEdit = 1;
				data.ind = index;
				this.initData = data;
				this.showPopup = true
			},
			getUnusedDetailMeth(id){
				getUnusedDetail(id).then(res=>{
					console.log("getUnusedDetail详情",res)
					if(res.code == 200){
						this.dataForm = res.data;
						res.data.contacts =res.data.contacts? res.data.contacts.map(item => ({
							...item,
							username: item.contactName,
							phone: item.contactPhone
						})):[]
						this.contactList = res.data.contacts
					}
				})
			},
			toAddMeth(data){
				this.dataForm.products.push(data);
				this.showPopup = false;
			},
			toEditMeth(data){
				this.dataForm.products[data.ind] = data;
				this.showPopup = false;
			},
			getCategoryListMeth() {
				getclassificationListApi().then(res => {
					console.log('getCategoryFirst', this.categoryOn)
					this.categoryList = res.data;
					this.itemList = this.categoryList[this.categoryOn].children;
					this.dataForm.goodCate = this.categoryList[this.categoryOn].id;
					this.dataForm.goodCateName = this.categoryList[this.categoryOn].name;
				})
			},
			toDel(index, info, callback) {
				if (info) {
					this.contactList.splice(index, 1, info);
				} else {
					this.contactList.splice(index, 1);
				}
				callback && callback();
			},
			clickMap(success) {
				uni.chooseLocation({
					success: (res) => {
						console.log(res)
						this.dataForm.address = res.address + res.name
						this.dataForm.latitude = res.latitude
						this.dataForm.longitude = res.longitude
						this.$forceUpdate();
						console.log("this.dataForm",this.dataForm)
						this.getBusiness({
							longitude: res.longitude,
							latitude: res.latitude,
						})
					},
				})
			},
			getBusiness(location) {
				let that = this;
				getApp().globalData.getBusinessList(location).then(res => {
					if (res.code == 200) {
						console.log('res',res)
						that.dataForm.province = res.data.province;
						that.dataForm.city = res.data.city;
						that.dataForm.district = res.data.district;
						that.dataForm.businessName = res.data.businessName;
						that.dataForm.businessCircle = res.data.businessId;
					}
				});
			},
			close() {
				this.showPopup = false
			},
			saveUnused(){
				let p = {...this.dataForm}
				if(!p.title){
					return this.$util.Tips({
						title: '请输入标题'
					});
				}
				p.contacts = this.contactList;
				p.status = this.unsedStatus;
				console.log(p)
				if(this.editId){
					editUnused(p).then(res=>{
						if(res.code == 200){
							this.$util.Tips({
								title: '编辑成功'
							}, {
								tab: 3
							})
						}
					})
				}else{
					addUnused(p).then(res=>{
						console.log(res)
						this.$util.Tips({
							title: '发布完成'
						}, {
							tab: 2,
							url: '/pages/releases/second-hand/second-hand'
						})
					})
				}
				
			},
			bindPickerChange(e) {
				this.dataForm.categorySon = this.itemList[e.detail.value].id
				this.dataForm.categorySonName = this.itemList[e.detail.value].name
				this.$forceUpdate();
				console.log('this.dataForm',this.dataForm)
			},
			typeNavClick(item, index) {
				// let title = '发布-'+this.categoryList[this.categoryOn].name+item.name
				// let title = '发布-'+item.name
				// uni.setNavigationBarTitle({
				// 	title:title
				// })
				this.typeOn = index
			},
			// 导航栏点击
			categoryNavClick(item, index) {
				// let title = '发布-'+item.name+this.types[this.typeOn].name
				// uni.setNavigationBarTitle({
				// 	title:title
				// })
				if (this.categoryList.length > 6) {
					var tempIndex = index - 2;
					tempIndex = tempIndex <= 0 ? 0 : tempIndex;
					this.tabLeft = (index - 2) * this.isWidth //设置下划线位置
				}
				this.categoryOn = index //设置导航点击了哪一个
				this.isLeft = index * this.isWidth //设置下划线位置
				this.itemList = this.categoryList[this.categoryOn].children ? this.categoryList[this.categoryOn].children :[];
				this.dataForm.goodsCate = item.id;
				this.dataForm.goodsCateName = item.name;
				this.dataForm.category = '';
				this.dataForm.categoryName = '';
			},
		}
	}
</script>

<style lang="scss">
	@import '/static/css/releaseForm.css';

	.nav-bg {
		background: #ffffff;
		padding: 18rpx 20rpx 36rpx;

		.item~.item {
			// margin-left: 48rpx;
		}

		.item {
			display: inline-block;
			white-space: nowrap;
			text-align: center;

			.img {
				width: 75rpx;
				height: 75rpx;
				border-radius: 50%;
				background: #ffffff;
				margin: 0 auto;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.name {
				display: inline-block;
				// min-width: 70rpx;
				font-size: 26rpx;
				font-weight: 700;
				line-height: 40rpx;
				color: rgba(0, 0, 0, 1);
				text-align: center;
			}

		}

		.item-on {
			.img {
				background: rgba(247, 247, 247, 1);
			}

			.name {
				position: relative;
				color: rgba(239, 39, 50, 1);
				font-weight: 700;

				&::after {
					content: '';
					position: absolute;
					left: 0;
					bottom: 0;
					width: 100%;
					height: 50%;
					background-color: rgba(239, 39, 50, .2);
					border-radius: 10rpx;
				}
			}
		}
	}

	.type-bg {
		padding: 24rpx 30rpx;
		background: rgba(247, 248, 250, 1);

		.type~.type {
			margin-left: 18rpx;
		}

		.type {
			padding: 6rpx 24rpx;
			border-radius: 10rpx;
			border: 1px solid rgba(229, 229, 229, 1);
			font-size: 32rpx;
			font-weight: 400;
			color: rgba(128, 128, 128, 1);
		}

		.type-on {
			border: 1px solid rgba(238, 33, 45, 1);
			background: rgba(238, 33, 45, 1);
			color: #ffffff;

		}
	}

	// 商品列表
	.goods-list {
		.goods:nth-child(3n-1) {
			margin: 0 18rpx;
		}

		.goods {
			width: 218rpx;
			margin-bottom: 18rpx;

			.img {
				width: 218rpx;
				height: 172rpx;
				background: #e7e7e7;

				image {
					width: 100%;
					height: 100%;
					border-radius: 4rpx;
				}
			}

			.title {
				width: 100%;
				font-size: 24rpx;
				font-weight: 400;
				line-height: 42rpx;
				color: rgba(0, 0, 0, 1);
				text-align: center;
			}

			.price {
				width: 100%;
				font-size: 24rpx;
				font-weight: 400;
				line-height: 42rpx;
				color: rgba(238, 33, 45, 1);
				text-align: center;
			}
		}
	}
</style>